package com.graduation.fq.teacher.process;

import com.graduation.fq.teacher.utils.Const;
import com.graduation.fq.teacher.utils.MsgIndex;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author jifang
 * @version 2016/5/22.
 */
public class MsgGetProcess extends BaseProcess {

    private static final List<String> urls = new ArrayList<>();

    static {
        urls.add("/message/new_count.do?t_id=%s");
        urls.add("/message/recv.do?t_id=%s");
    }


    /**
     * request
     */
    private MsgIndex index;
    private String tId;

    /**
     * response
     */
    private Map<String, String> messages;
    private Integer count;

    public MsgGetProcess(MsgIndex index, String tId) {
        this.index = index;
        this.tId = tId;
    }

    public MsgIndex getIndex() {
        return index;
    }

    public void setIndex(MsgIndex index) {
        this.index = index;
    }

    public String gettId() {
        return tId;
    }

    public void settId(String tId) {
        this.tId = tId;
    }

    public Map<String, String> getMessages() {
        return messages;
    }

    public void setMessages(Map<String, String> messages) {
        this.messages = messages;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    @Override
    String getRequestUrl() {
        return String.format(urls.get(getIndex().getIndex()), gettId());
    }

    @Override
    String getParameter() {
        return null;
    }

    @Override
    void onResult(JSONObject object) {
        int statusCode = object.optInt("status");
        setStatus(statusCode);
        if (statusCode == Const.HTTP_SUCCESS) {
            if (getIndex() == MsgIndex.NEW_COUNT) {
                setCount(object.optInt("data"));
            } else {
                String json = object.optJSONObject("data").toString();
                Map map = com.alibaba.fastjson.JSONObject.parseObject(json, Map.class);
                setMessages(map);
            }
        }
    }
}
